package com.zf.yichat.service.impl;

import com.zf.yichat.mapper.StLowCarbonKnowledgeMapper;
import com.zf.yichat.model.StLowCarbonKnowledge;
import com.zf.yichat.model.UserPrintTreeTask;
import com.zf.yichat.service.PlantTreeService;
import com.zf.yichat.service.StCarbonService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class StCarbonServiceImpl implements StCarbonService {

    @Autowired
    private StLowCarbonKnowledgeMapper stLowCarbonKnowledgeMapper;

    @Autowired
    private PlantTreeService plantTreeService;

    @Override
    public FsResponse selectKnowledgeList() {

        Example example = new Example(StLowCarbonKnowledge.class);
        example.setOrderByClause("create_time desc");
        return DtoChangeUtils.getPageList(stLowCarbonKnowledgeMapper.selectByExample(example), knowledge -> {
            StLowCarbonKnowledge carbonKnowledge = new StLowCarbonKnowledge();
            BeanUtils.copyProperties(knowledge, carbonKnowledge);
            return carbonKnowledge;
        });
    }

    @Override
    public StLowCarbonKnowledge selectKnowledgeById(Long id) {
        return stLowCarbonKnowledgeMapper.selectByPrimaryKey(id);
    }

    @Override
    public FsResponse saveKnowledge(StLowCarbonKnowledge knowledge) {
        Integer knowledgeId = knowledge.getId();
        if (null == knowledgeId) {
            String details = knowledge.getDetails();
            details = buildHtml(details);
            knowledge.setDetails(details);
            knowledge.setCreateTime(new Date());
            return FsResponseGen.successData(stLowCarbonKnowledgeMapper.insertSelective(knowledge));
        }
        return FsResponseGen.successData(stLowCarbonKnowledgeMapper.updateByPrimaryKeySelective(knowledge));
    }

    @Override
    public FsResponse deleteKnowledge(Integer id) {
        return FsResponseGen.successData(stLowCarbonKnowledgeMapper.deleteByPrimaryKey(id));
    }

    @Override
    public FsResponse addNumbers(Long userId, Integer num) {

        plantTreeService.addTask(userId, "系统赠送", UserPrintTreeTask.SYSTEM_TASK, num);
        return FsResponseGen.successMsg("赠送成功！");
    }


    /**
     * 组装html
     * @param content
     * @return
     */
    public static String buildHtml (String content) {
        StringBuilder sb = new StringBuilder ();
        sb.append ("<!DOCTYPE html>");
        sb.append ("<head>");
        sb.append ("<meta charset=\"UTF-8\">");
        sb.append ("<meta name=\"renderer\"content=\"webkit\">");
        sb.append ("<meta http-equiv=\"X-UA-Compatible\"content=\"IE=edge,chrome=1\">");
        sb.append ("<meta name=\"viewport\"content=\"width=device-width, initial-scale=1, maximum-scale=1\">");
        sb.append ("<title >低碳知识</title>");
        sb.append ("</head>");
        sb.append ("<body>");
        sb.append (content);
        sb.append ("</body>");
        sb.append ("</html>");
        return sb.toString ();
    }
}
